package Web;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wuhan
 * @date 2019/10/20 14:06
 */
// 172.20.10.11
class PicClient{
    public static void main(String[] args) throws IOException {

        if (args.length!=1){
            System.out.println("Please choose a jpg");
            return;
        }
        File file =new File(args[0]);
        if (file.exists()&&file.isFile()){
            System.out.println("File have problem");
            return;
        }
        if (!file.getName().endsWith(".jpg")){
            System.out.println("style wrong");
            return;
        }
        if (file.length()>1024*1024*5){
            System.out.println("Too large");
            return;
        }

        Socket s = new Socket("172.20.10.11",10007);

        FileInputStream fis = new FileInputStream(file);
        OutputStream out = s.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len=fis.read(buf))!=-1){
            out.write(buf,0,len);
        }
        s.shutdownOutput();
        InputStream in = s.getInputStream();
        byte[] bufIn = new byte[1024];
        int read = in.read(bufIn);
        System.out.println(new String(bufIn,0,read));
        fis.close();
        s.close();
    }
}
public class ImageDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10007);
        Socket s = ss.accept();
        InputStream in = s.getInputStream();
        FileOutputStream fos = new FileOutputStream("server.jpg");
        byte[] buf = new byte[1024];
        int len = 0;

        while ((len = in.read(buf))!=-1){
            fos.write(buf,0,len);
        }
        OutputStream out = s.getOutputStream();
        out.write("上传成功".getBytes());
        fos.close();
        s.close();
        ss.close();

    }
}
